package ink.xiaobaibai.serviceOxy.buy.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ink.xiaobaibai.bo.DeviceMqttBo;
import ink.xiaobaibai.entity.BuyOxygenDevice;
import ink.xiaobaibai.entity.BuyOzoneDevice;
import ink.xiaobaibai.service.IBuyOxygenDeviceService;
import ink.xiaobaibai.service.IBuyOzoneDeviceService;
import ink.xiaobaibai.serviceOxy.IMqttService;
import ink.xiaobaibai.serviceOxy.buy.IDeviceBuyControlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description: 购买机器服务层
 * @author: 小白白
 * @create: 2021-06-14
 **/

@Service
public class DeviceBuyControlServiceImpl implements IDeviceBuyControlService {

    @Autowired
    private IBuyOzoneDeviceService buyOzoneDeviceService;

    @Autowired
    private IBuyOxygenDeviceService buyOxygenDeviceService;

    @Autowired
    private IMqttService mqttService;

    @Override
    public void open(Integer deviceId, Integer seconds) {
        BuyOzoneDevice ozoneDevice = this.buyOzoneDeviceService.getById(deviceId);
        BuyOxygenDevice oxygenDevice = this.buyOxygenDeviceService.getById(ozoneDevice.getOxygenId());
        this.mqttService.openDevice(ozoneDevice.getDeviceCode(), new DeviceMqttBo(ozoneDevice.getDeviceCode(), true, ozoneDevice.getDelayedTime(), ozoneDevice.getFanTime(), ozoneDevice.getValveTime(), seconds));
        this.mqttService.openDevice(ozoneDevice.getDeviceCode(), new DeviceMqttBo(oxygenDevice.getDeviceCode(), true, oxygenDevice.getDelayedTime(), oxygenDevice.getFanTime(), oxygenDevice.getValveTime(), seconds));
    }

    @Override
    public void close(Integer deviceId) {
        QueryWrapper<BuyOzoneDevice> q1 = new QueryWrapper<>();
        q1.select("device_code", "oxygen_id");
        q1.eq("id", deviceId);
        BuyOzoneDevice ozoneDevice = this.buyOzoneDeviceService.getOne(q1);
        QueryWrapper<BuyOxygenDevice> q2 = new QueryWrapper<>();
        q2.select("device_code");
        q2.eq("id", ozoneDevice.getOxygenId());
        BuyOxygenDevice oxygenDevice = this.buyOxygenDeviceService.getOne(q2);
        this.mqttService.closeDevice(ozoneDevice.getDeviceCode());
        this.mqttService.closeDevice(oxygenDevice.getDeviceCode());
    }

}
